//
//  GameStatusBar.swift
//  1010
//
//  Created by 米明 on 15/7/23.
//  Copyright (c) 2015年 米明. All rights reserved.
//

import UIKit
//游戏状态栏
//计分板
//倒计时
//暂停按钮...
class GameStatusBar: UIView {
    
    var pause:UIButton!//暂停按钮
    var soundSwitch:UIButton!//音乐开关
    var bestScore:UILabel!//最高分
    var nowScore:UILabel!//当前分数
    //var countDown:UIButton!//倒计时 //暂停
    
    let Width = CGFloat(60)
    
    var pauseCallBack: CallBack!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.alpha=0
        
        pause = produceButton(frame: CGRect(x: 0, y: 0, width: Width, height: Width))
        pause.setImage(UIImage(named: "pause"), for: UIControl.State.normal)
        pause.addTarget(self, action: #selector(pauseAction(_:)), for: UIControl.Event.touchUpInside)
        
        soundSwitch = produceCustomButton(frame: CGRect(x: SCREEN_SIZE.width - Width, y: 0, width: Width, height: Width))
        soundSwitch.setImage(UIImage(named: "m_on"), for: .normal)
        soundSwitch.setImage(UIImage(named: "m_off"), for: .selected)
        soundSwitch.isSelected = !DataCenter.getMusicSwitch()
        soundSwitch.addTarget(self, action: #selector(soundSeitchAction(_:)), for: UIControl.Event.touchUpInside)
        
        let imageV = produceButton(frame: CGRect(x: 0, y: 0, width: Width, height: Width))!
        imageV.tintColor=UIColor.white
        imageV.center=CGPoint(x: SCREEN_SIZE.width/2.0, y: Width/2.0)
        imageV.setImage(UIImage(named: "c_small"), for: UIControl.State.normal)
        self.addSubview(imageV)

        bestScore = produceLabel(frame: CGRect(x: 0, y: 0, width: Width*2, height: Width))
        bestScore.center = CGPoint(x: imageV.frame.origin.x-Width, y: Width/2.0)
        bestScore.font=UIFont(name: FONT_STANK, size: 24)
        bestScore.text="\(DataCenter.getBestScore())"
        bestScore.textColor=LYColor.ScoreColor()
        //bestScore.backgroundColor=UIColor.redColor()
        bestScore.textAlignment=NSTextAlignment.right
        
        nowScore = produceLabel(frame: CGRect(x: 0, y: 0, width: Width*2, height: Width))
        nowScore.center = CGPoint(x: imageV.frame.origin.x+2*Width, y: Width/2.0)
        nowScore.font=UIFont(name: FONT_STANK, size: 24)
        nowScore.textAlignment=NSTextAlignment.left
        //nowScore.backgroundColor=UIColor.redColor()
        
        //countDown = produceButton(CGRectMake(-Width, 0, Width, Width))
        
        show()
    }
    
    func show() {
        UIView.animate(withDuration: 0.4, animations: {
            self.alpha=1
        }) { (finished) -> Void in
            
        }
    }
    
    @objc func soundSeitchAction(_ sender:UIButton) {
        if soundSwitch.isSelected == true {
            soundSwitch.isSelected = false
            DataCenter.setMusicSwitch(isOn: true)
            LYSoundManager.manager.playDefaultBGM()
            
        }else{
            soundSwitch.isSelected = true
            DataCenter.setMusicSwitch(isOn: false)
            LYSoundManager.manager.stopMusic()
        }
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.2, animations: {
            self.alpha=0
        }) { (finished) -> Void in
            self.removeFromSuperview()
        }
    }
    
    //MARK: 回调
    func pauseActionBack(callBack:@escaping ()->()) {
        pauseCallBack = callBack
    }
    @objc func pauseAction(_ btn:UIButton) {
        pauseCallBack()
    }
    
    //更新计分板
    func reloadScore() {
        self.nowScore.text="\(DataCenter.nowScore())"
    }
    
    func reloadBestScore() {
        self.bestScore.text="\(DataCenter.getBestScore())"
    }
    

    //MARK: 生产button
    func produceButton(frame: CGRect) -> UIButton! {
        let btn = UIButton(type: UIButton.ButtonType.system)
        btn.frame=frame
        btn.layer.masksToBounds=true
        btn.layer.cornerRadius=SPACE
        btn.tintColor=UIColor.white
        self.addSubview(btn)
        return btn
    }
    
    func produceCustomButton(frame: CGRect) -> UIButton! {
        let btn = UIButton(type: UIButton.ButtonType.custom)
        btn.frame=frame
        btn.layer.masksToBounds=true
        btn.layer.cornerRadius=SPACE
        btn.tintColor=UIColor.white
        self.addSubview(btn)
        return btn
    }
    
    func produceLabel(frame: CGRect) -> UILabel! {
        let btn = UILabel(frame: frame)
        btn.frame=frame
        btn.textAlignment=NSTextAlignment.center
        btn.layer.masksToBounds=true
        btn.layer.cornerRadius=SPACE
        btn.textColor=UIColor.white
        self.addSubview(btn)
        return btn
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
